package jkCore.social.ad.config;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.util.Log;

import jkCore.social.SocialConfig;
import jkCore.social.ad.adapter.AdBaseAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Locale;

public class AdConfig {

    public static final int BANNER_REQUEST_INTERVAL = 1000;
    private static final String TAG = "AdConfig";

    public JSONObject videoParams = null;
    public JSONObject interParams = null;
    private Context mContext;
    private List<AdBaseAdapter> mAdapters;

    private static AdConfig ins = null;

    public static AdConfig getIns() {
        if (ins == null) {
            ins = new AdConfig();
        }
        return ins;
    }

    public void requestConfig(final Context context, String appKey, List<AdBaseAdapter> adapters) {
        mContext = context;
        mAdapters = adapters;
        parseAdData(appKey);
    }

    private void parseAdData(String adData) {
        for (AdBaseAdapter adapter : mAdapters) {
            if (adapter.adName().equals("MAX")) {
                try {
                    ApplicationInfo appInfo = mContext.getPackageManager().getApplicationInfo(mContext.getPackageName(), PackageManager.GET_META_DATA);
                    Log.d(TAG, "parseAdData appInfo: " + appInfo.toString());

                    adapter.mConfigValue.appId = SocialConfig.AppLovinAppId;
                    adapter.mConfigValue.appKey = SocialConfig.AppLovinAppKey;
                    adapter.mConfigValue.bannerKey = SocialConfig.AppLovinBannerKey;
                    adapter.mConfigValue.interKey = SocialConfig.AppLovinInterKey;
                    adapter.mConfigValue.videoKey = SocialConfig.AppLovinVideoKey;
                    adapter.mConfigValue.nativeKey = SocialConfig.AppLovinNativeKey;
                    adapter.mConfigValue.floatKey = SocialConfig.AppLovinFloatKey;
                    adapter.mConfigValue.boxKey = SocialConfig.AppLovinBoxKey;
                    adapter.mConfigValue.splashKey = SocialConfig.AppLovinSplashKey;
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        interParams = getDefaultInterParams();
        videoParams = getDefaultVideoParams();
    }

    private JSONObject getDefaultInterParams() {
        try {
            JSONObject defaultPlacement = new JSONObject();
            defaultPlacement.put("Type", 1);
            defaultPlacement.put("Param", 2);
            JSONObject myinterParams = new JSONObject();
            myinterParams.put("default", defaultPlacement);
            return myinterParams;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject getDefaultVideoParams() {
        try {
            JSONObject defaultPlacement = new JSONObject();
            defaultPlacement.put("ItemID", 0);
            defaultPlacement.put("Amount", 1);
            JSONArray array = new JSONArray();
            array.put(defaultPlacement);
            JSONObject myvideoParams = new JSONObject();
            myvideoParams.put("default", array);
            return myvideoParams;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    private String readAssetFile(Context context, String filename) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(filename)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
            return stringBuilder.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    private String getLocal() {
        return Locale.getDefault().getCountry();
    }
}
